package top.chmin.project.cloud.builder.config.info.auto;

// field import auto

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.entity.Page;

import java.util.List;
import java.util.stream.Stream;

/**
 * chmin
 * 2023-12-25 09:05:44
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ConfigInfoService {

    @Resource
    ConfigInfoPlugin plugin;

    @Resource
    ConfigInfoDao configInfoDao;

// field labelDao auto

    public int save(ConfigInfoSave.Param param) {
        plugin.checkSave(param);
        ConfigInfo configInfo = BeanUtil.toBean(param, ConfigInfo.class);
        plugin.beforeSave(configInfo);
        int result = configInfoDao.save(configInfo);
        plugin.afterSave(param, configInfo, result);
        return result;
    }

    public int updateById(ConfigInfoUpdateById.Param param) {
        plugin.checkUpdateById(param);
        ConfigInfo configInfo = BeanUtil.toBean(param, ConfigInfo.class);
        plugin.beforeUpdateById(configInfo);
        int result = configInfoDao.updateById(configInfo);
        plugin.afterUpdateById(param, configInfo, result);
        return result;
    }

    public int deleteById(ConfigInfoDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = configInfoDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public ConfigInfoSelectById.Result selectById(ConfigInfoSelectById.Param param) {
        plugin.checkSelectById(param);
        ConfigInfoFPC fpc = new ConfigInfoFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        ConfigInfoSelectById.Result result = configInfoDao.getFirst(fpc.toCondition(), ConfigInfoSelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<ConfigInfoSelectPage.Result> selectPage(ConfigInfoSelectPage.Param param) {
        plugin.checkSelectPage(param);
        ConfigInfoFPC fpc = new ConfigInfoFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<ConfigInfoSelectPage.Result> result = configInfoDao.getPageByCondition(fpc.toCondition(), ConfigInfoSelectPage.Result.class);
// field label auto
        plugin.afterSelectPage(param, result);
        return result;
    }

    public List<ConfigInfoSelect.Result> select(ConfigInfoSelect.Param param) {
        plugin.checkSelect(param);
        ConfigInfoFPC fpc = new ConfigInfoFPC();
        plugin.beforeSelect(param, fpc);
        Stream<ConfigInfoSelect.Result> result = configInfoDao.getStreamByCondition(fpc.toCondition(), ConfigInfoSelect.Result.class)
                .peek(r -> {
                    r.setValue(r.getId());
                    r.setLabel(r.getConfigName());
                });
        List<ConfigInfoSelect.Result> list = result.toList();
        plugin.afterSelect(param, list);
        return list;
    }


// entity search auto

}
